define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'smallappversion/index',
                    add_url: 'smallappversion/add',
                    edit_url: 'smallappversion/edit',
                    del_url: 'smallappversion/del',
                    multi_url: 'smallappversion/multi',
                    table: 'smallapp_version',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'merid', title: __('Merid')},
                        {field: 'template_id', title: __('Template_id')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'status', title: __('Status')},
                        {field: 'status_text', title: __('Status'), operate: false},
                        {field: 'auditid', title: __('Auditid')},
                        {field: 'fail_reason', title: __('Fail_reason')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {name: 'code', text: '体验二维码', title: '体验二维码', icon: '', classname: 'btn btn-xs btn-success btn-dialog', url: 'smallappversion/code',},
                                {name: 'submit', text: '提交审核', title: '提交审核', icon: '', classname: 'btn btn-xs btn-warning btn-dialog', url: 'smallappversion/submit',},
                                {name: 'cancel', text: '撤销审核', title: '撤销审核', icon: '', classname: 'btn btn-xs btn-danger btn-ajax', url: 'smallappversion/cancel', confirm: '确定撤销审核吗？', success:function(){table.bootstrapTable('refresh', {});}},
                                {name: 'refreshStatus', text: '刷新审核状态', title: '刷新审核状态', icon: '', classname: 'btn btn-xs btn-danger btn-ajax', url: 'smallappversion/refreshStatus', success:function(){table.bootstrapTable('refresh', {});}},
                                {name: 'release', text: '发布代码', title: '发布代码', icon: '', classname: 'btn btn-xs btn-success btn-ajax', url: 'smallappversion/release', success:function(){table.bootstrapTable('refresh', {});}},
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if(row.status == 0){
                                    $(table).data("operate-cancel", null);
                                    $(table).data("operate-refreshStatus", null);
                                    $(table).data("operate-release", null);
                                }

                                if(row.status == 1){
                                    $(table).data("operate-submit", null);
                                    $(table).data("operate-submit", null);
                                    $(table).data("operate-release", null);
                                }

                                if(row.status == 2){
                                    $(table).data("operate-cancel", null);
                                    $(table).data("operate-refreshStatus", null);
                                    $(table).data("operate-release", null);
                                }

                                if(row.status == 3){
                                    $(table).data("operate-submit", null);
                                    $(table).data("operate-cancel", null);
                                    $(table).data("operate-refreshStatus", null);
                                }

                                if(row.status == 4){
                                    $(table).data("operate-submit", null);
                                    $(table).data("operate-cancel", null);
                                    $(table).data("operate-refreshStatus", null);
                                    $(table).data("operate-release", null);
                                }

                                if(row.status == 5){
                                    $(table).data("operate-cancel", null);
                                    $(table).data("operate-refreshStatus", null);
                                    $(table).data("operate-release", null);
                                }

                                $(table).data("operate-del", null);
                                $(table).data("operate-edit", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        code: function () {
            Controller.api.bindevent();
        },
        submit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});